$(function () {
    let markid = getUrlString('markid');
    let id = getUrlString('id');
    let kutoysid = getUrlString('kutoysids');
    let kutoysids = kutoysid.replace(/,/g,';');
    let name = getUrlString('name');
    let phone = getUrlString('phone');
    let adress = getUrlString('adress');
    let local1name = getUrlString('local1name');
    let local2name = getUrlString('local2name');
    let local3name = getUrlString('local3name');
    let local1 = getUrlString('local1');
    let local2 = getUrlString('local2');
    let local3 = getUrlString('local3');
    let kdtype = 0,yfMoney,issfpack,isSfCup=false,isBYCup = false,totalcost = 0;//快递选择,运费
    // alert(kutoysids)
    //判断是否有包邮卷

    //快递停发通知
    function stopKd() {
        let data = {
            'markid':markid
        };
        getData('post','/niu/apiv4/niu_fahuo_message.php',data,function (data) {
            console.log(data);
            $('.stopSendTitle').html(data.title);
            $('.stopSendData').html(data.data)
        })
    }

    stopKd()

    function getMonths() {
        let date = new Date();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        if (month === 1 && 1<= day && day <= 3){
            $('.stopSend').show();
        } else {
            $('.stopSend').show();
        }
    }

    getMonths();

    function cupList() {
        let data = {
            'markid':markid,
            'type':0,
        };
        getData('post','/niu/apiv4/fahuo_dosend_my_coupon.php',data,function (data) {
            console.log(data);
            //包邮劵
            let cupNum = Number(data.info[0].num);
            if (issfpack === '0'){
                if (cupNum > 0){
                    $('#Hide,.noCheck').hide();
                    $('.coupons,#orderShow,.check').show();
                }else {
                    isBYCup = true;
                    $('.check,.noCheck').hide();
                    $('.coupons').show();
                    $('.noCup').show().html('暂无可用')
                }
            }
            //顺丰保价劵
            let sfCupNum = Number(data.sfnum);
            if (sfCupNum > 0){
                $('#Hide').hide();
                $('.sfnoCheck').show();
            }else {
                isSfCup = true;
                $('.sfcheck,.sfnoCheck').hide();
                $('.sfnoCup').show().html('暂无可用')
            }
        })
    }

    //不使用包邮劵
    $('.check').click(function () {
        $(this).hide();
        $('#orderShow,#sfpayYun').hide();
        $('.noCheck,#payYun').show();
        if (isSfCup){
            $('.sfnoCheck').hide();
        } else {
            $('.sfnoCheck').show();
        }
    });
    //使用包邮劵
    $('.noCheck').click(function () {
        $(this).hide();
        $('#payYun,.sfcheck,#sfpayYun').hide();
        $('.check,#orderShow').show();
        if (isSfCup){
            $('.sfnoCheck').hide();
        } else {
            $('.sfnoCheck').show();
        }
    });

    //不使顺丰保价劵
    $('.sfcheck').click(function () {
        $(this).hide();
        $('#sfpayYun,#orderShow').hide();
        if (issfpack === '1'){
            $('#baoYou').show().html('包邮');
            $('.sfnoCheck').show();
            return
        }
        $('.sfnoCheck,#payYun').show();
        if (isBYCup){
            $('.noCheck').hide();
        } else {
            $('.noCheck').show();
        }
    });
    //使用顺丰保价劵
    $('.sfnoCheck').click(function () {
        $(this).hide();
        $('#payYun,.sfcheck,.check,#orderShow').hide();
        $('.sfcheck,#sfpayYun').show();
        if (issfpack === '1' || issfpack === '3'){
            return
        }
        if (isBYCup){
            $('.noCheck').hide();
        } else {
            $('.noCheck').show();
        }
    });

    //判断是否有地址
    if (id === null){
        $('#address').hide();
        $('#noAddress,#Hide').show();
        //添加地址
        $('#noAddress .addBtn').click(function () {
            window.location.href = '../../view/addressAdmin/addressAdmin.html?markid='+markid+'&kutoysids='+kutoysids+'&source=shipments'
        })
    }else {
        $('#noAddress,#Hide').hide();
        $('#userNick').html(name);
        $('#phone').html(phone);
        $('#addressDetail').html(local1name+local2name+local3name+adress);
        $('#address').show().click(function () {
            window.location.href = '../../view/addressAdmin/addressAdmin.html?markid='+markid+'&kutoysids='+kutoysids+'&source=shipments'
        });
        issfsend();
    }

    //使用包邮劵提交订单
    $('#orderShow').click(function () {
        baoyou(kdtype,0);
    });

    //使用顺丰保价劵提交订单
    $('#sfpayYun').click(function () {
        baoyou(3,2)
    });

    function baoyou(kdtype,type) {
        let usercomment = $('#userRemake').val();
        if (id === null){
            $.toast('请添加地址')
        }
        let data = {
            'markid':markid,
            'adid':id,
            'goodsids':kutoysids,
            'kdtype':kdtype,
            'type':type,
            'usercomment':usercomment
        };
        getData('post','/niu/apiv4/fahuo_baoyou.php',data,function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                $.toast(data.msg);
                setTimeout(function () {
                    nativeCommand("dancool://activity=reflashfin");
                },500)
            } else {
                $.toast(data.msg)
            }
        })
    }

    //判断运费
    function checkyun (){
        let data = {
            'markid':markid,
            'local1':local1,
            'local2':local2,
            'local3':local3,
        };
        getData('post','/niu/apiv3/fahuo_checkyun.php',data,function (data) {
            console.log(data);
            yfMoney = data.price;
            $('#payYun').html('支付运费'+yfMoney+'元')
        })
    }

    //选择快递
    $('#expressage').click(function () {
        if (issfpack === '3'){
            return
        }
        if (totalcost <= 50){
            return;
        }
        var buttons1 = [
            {
                text: '默认快递',
                bold: true,
                onClick: function () {
                    $('#expressage').html('默认快递');
                    if (issfpack === '0'){
                        cupList();
                        $('#payYun').show().html('支付运费'+yfMoney+'元');
                        $('#Hide,#baoYou,#SFbj,.check,#orderShow,.noCup,.sfnoCup').hide();
                        kdtype = 0;
                    } else if (issfpack === '1'){
                        $('#Hide,#payYun,#SFbj,.noCheck,.check,#orderShow').hide();
                        $('.noCup').show().html('该订单包邮');
                        kdtype = 0;
                        $('#baoYou').html("包邮");
                    }else if (issfpack === '3'){
                        $('#Hide,#payYun,#SFbj,.noCheck,.check,#orderShow').hide();
                        $('.noCup').show().html('该订单包邮');
                        kdtype = 0;
                        $('#baoYou').html("顺丰包邮");
                    }



                }
            },
            {
                text: '顺丰到付',
                onClick: function () {
                    $('#expressage').html('顺丰到付');
                    $('#Hide,#payYun,#SFbj,.noCheck,.check,.sfnoCheck,.sfcheck,#orderShow').hide();
                    $('.noCup').show().html('不可用');
                    $('.sfnoCup').show().html('不可用');
                    kdtype = 1;
                    if (issfpack === '0'){
                        $('#baoYou').show().html("顺丰到付");
                    }else {
                        $('#baoYou').html("顺丰到付");
                    }


                    // issfsend()
                }
            },
            {
                text: '返回',
                onClick: function () {

                }
            }];
        $.modal({
            title: '选择快递',
            verticalButtons: true,
            buttons:buttons1
        });
    });

    //判断是否顺丰保价
    function issfsend() {
        let data = {

            'markid':markid,
            'goodsids':kutoysids
        };
        getData('post','/niu/apiv3/fahuo_issfsend.php',data,function (data) {
            console.log(data);
            if (local2 === '361100' || local2 === '360100'){
                $('.sfcoupons').hide();

                //如果顺丰保价就包邮
                if (data.data.issfpack === '3' || data.data.issfpack === '1'){
                    issfpack = '1'
                } else {
                    issfpack = '0'
                }

            } else {
                issfpack = data.data.issfpack;
            }
            if (issfpack === '0'){
                $('.expressage').show();
                $('#payYun').show();
                cupList();//调用是否有包邮劵
                checkyun();
            }else if(issfpack === '1'){
                $('#baoYou').show().html('包邮');
                $('.coupons .check,.coupons .noCheck,.sfcheck').hide();
                $('.sfnoCheck').show();
                $('.coupons .noCup').show().html('该订单包邮');
                cupList();//调用是否有包邮劵

            }else if(issfpack === '3'){
                $('#baoYou').show().html('顺丰包邮');
                $('.coupons .check,.coupons .noCheck,.sfcoupons .sfcheck,.sfcoupons .sfnoCheck').hide();
                $('.coupons .noCup,.sfcoupons .sfnoCup').show().html('该订单包邮');
            }

        })
    }


    //包邮（顺丰包邮）提交订单
    $('#baoYou').click(function () {
        let usercomment = $('#userRemake').val();
        if (id === null){
            $.toast('请添加地址')
        }
        let data = {
            'markid':markid,
            'adid':id,
            'goodsids':kutoysids,
            'kdtype':kdtype,
            'usercomment':usercomment
        };
        getData('post','/niu/apiv3/fahuo_dosend.php',data,function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                $.toast(data.msg);
                setTimeout(function () {
                    nativeCommand("dancool://activity=reflashfin");
                },500)
            } else {
                $.toast(data.msg)
            }
        })
    });

    //支付运费
    $('#payYun').click(function () {
        let usercomment = $('#userRemake').val();
        if (id === null){
            $.toast('请添加地址')
        }
        let data = {
            'markid':markid,
            'adid':id,
            'goodsids':kutoysids,
            'kdtype':kdtype,
            'usercomment':usercomment
        };
        getData('post','/niu/apiv3/fahuo_tmp_dosend.php',data,function (data) {
            console.log(data);

            if (data.resultcode === '0'){
                let paycode = data.paycode;
                nativeCommand("dancool://paymoney2="+paycode);
            } else {
                $.toast(data.msg)
            }
        })
    });

    //发货商品列表
    function shipmentsList() {

        let data = {
            'markid':markid,
            'kutoysids':kutoysids
        };
        getData('post','/niu/apiv3/fahuo_getkutoylist.php',data,function (data) {
            console.log(data);
            let append = '';
            totalcost = data.totalcost;
            $(data.data).each(function () {
                append += '<li>' +
                    '            <input type="hidden" value="'+this.id+'"> '+
                    '            <div class="pic">' +
                    '                <img src="'+this.pics+'" alt="">' +
                    '            </div>' +
                    '            <div class="right">'+this.name+'</div>' +
                    '        </li>'
            });

            $('#shipmentsList').append(append);
            $("#shipmentsList li").click(function () {
                let id = $(this).find('input').val();
                window.location.href = 'dancool://fullweb='+urlView+'danji/commodityDetail.html?id='+id;
                // window.location.href = 'dancool://toysid='+id;
            })
        })
    }
    shipmentsList();

    $('.bottomJt').click(function () {
        if ($(this).hasClass('up')){
            $('.bottomJt').removeClass('up');
            $('.bottomJt img').css({
                transform: 'rotate(0deg)'
            });
            $('.shipmentInfo').css('height','8rem')
        } else {
            $('.bottomJt').addClass('up');
            $('.bottomJt img').css({
                transform: 'rotate(180deg)'
            });
            $('.shipmentInfo').css('height','initial')
        }

    });

    $('.cancel').click(function () {
        nativeCommand("dancool://activity=fin")
    });

});
